package com.yyz.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyz.lease.common.constant.RedisConstant;
import com.yyz.lease.common.utils.ThreadLocalUtil;
import com.yyz.lease.model.entity.*;
import com.yyz.lease.web.app.mapper.*;
import com.yyz.lease.web.app.service.ApartmentInfoService;
import com.yyz.lease.web.app.service.BrowsingHistoryService;
import com.yyz.lease.web.app.service.RoomInfoService;
import com.yyz.lease.web.app.vo.apartment.ApartmentItemVo;
import com.yyz.lease.web.app.vo.attr.AttrValueVo;
import com.yyz.lease.web.app.vo.fee.FeeValueVo;
import com.yyz.lease.web.app.vo.graph.GraphVo;
import com.yyz.lease.web.app.vo.room.RoomDetailVo;
import com.yyz.lease.web.app.vo.room.RoomItemVo;
import com.yyz.lease.web.app.vo.room.RoomQueryVo;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    private final RoomInfoMapper roomInfoMapper;
    private final ApartmentInfoService apartmentInfoService;
    private final GraphInfoMapper graphInfoMapper;
    private final AttrValueMapper attrValueMapper;
    private final FacilityInfoMapper facilityInfoMapper;
    private final LabelInfoMapper labelInfoMapper;
    private final PaymentTypeMapper paymentTypeMapper;
    private final FeeValueMapper feeValueMapper;
    private final LeaseTermMapper leaseTermMapper;
    private final BrowsingHistoryService browsingHistoryService;

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页查询房间信息
     */
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    /**
     * 根据id获取房间的详细信息
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        String key = RedisConstant.APP_ROOM_PREFIX + id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) redisTemplate.opsForValue().get(key);
        if (roomDetailVo != null) {
            return roomDetailVo;
        }

        RoomInfo roomInfo = getById(id);

        //所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());

        //图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphInfosByRoomId(id);

        //属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValuesByRoomId(id);

        //配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfosByRoomId(id);

        //标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfosByRoomId(id);

        //支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypesByRoomId(id);

        //杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVosByRoomId(roomInfo.getApartmentId());

        //租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermsByRoomId(id);

        //拼接数据
        roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //保存到缓存
        redisTemplate.opsForValue().set(key,roomDetailVo);

        //保存浏览记录
        Claims claims = ThreadLocalUtil.get();
        Long userId = claims.get("userId", Long.class);
        browsingHistoryService.saveHistory(userId, id);

        return roomDetailVo;
    }

    /**
     * 根据公寓id分页查询房间信息
     */
    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }


}




